//
//  ShopCart.m
//  HuiPinzhijia
//
//  Created by LLMPro on 16/8/29.
//  Copyright © 2016年 LLMPro. All rights reserved.
//

#import "ShopCart.h"
#import "NoThingShopView.h"
#import "HomePage.h"
#import "ShopHeaderView.h"
#import "ShotTableViewCell.h"
#import "AddressViewController.h"
@interface ShopCart ()<NoThingShopViewDelegate,UITableViewDataSource,UITableViewDelegate,ShotTableViewCellDelegate>
@property (nonatomic, strong)UITableView *shopTableView;
@property (nonatomic, strong)NSMutableArray *shopingArr;
@property (nonatomic, assign)NSInteger indext;
@property (nonatomic, assign)NSInteger allPrices;
@property (nonatomic, strong)NSMutableArray *allpriceArr;
@end

@implementation ShopCart
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tabBarController.tabBar.hidden=YES;
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self updateData];
    [self noThingView];
    [self initMyNavigationBar];
    
    self.allpriceArr = [[NSMutableArray alloc]init];
    

}

#pragma mark -- NothingView
- (void)noThingView
{
    if (_shopingArr.count != 0)
        //如果购物车有东西
    {
        NoThingShopView *nothing = [NoThingShopView sharedNoThingView];
        [nothing removeFromSuperview];
        [self.view addSubview:self.shopTableView];
        
    }
    else{
        
        NoThingShopView *shopV = [NoThingShopView sharedNoThingView];
        shopV.frame = self.view.frame;
        shopV.delegate = self;
        [shopV updateNothingView];
        [self.view addSubview:shopV];

    }
    
}
#pragma mark -- 伪数据
- (void)updateData
{
    for (int i = 0; i < 5; i++)
    {
        [self.shopingArr addObject:@"sss"];
    }
    [self.shopTableView reloadData];
    
}
#pragma NoThingView Delegate
- (void)noThingBtnClick
{
    self.tabBarController.selectedIndex = 0;
}

#pragma mark -- 自定制NavigationBar
- (void)initMyNavigationBar
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
    view.backgroundColor = [UIColor blueColor];
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 16, 32, 42)];
    [btn setImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(navigationBarBtnC) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    [self.view addSubview:view];
}
//返回首页
- (void)navigationBarBtnC
{
     self.tabBarController.selectedIndex = 0;
}

#pragma mark -- TableView Delegate

//这个方法返回指定的 row 的高度。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 160;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.shopingArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShotTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SHOTCELL"];
    _indext = indexPath.row;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell updateMyCellWithModel:nil];
    cell.delegate = self;
    [_allpriceArr addObject:cell.priceL.text];
    
    return cell;
}
#pragma maek -- ShopCell Delegate
- (void)deleteTheShop
{
    [self.shopingArr removeObjectAtIndex:_indext];
    dispatch_async(dispatch_get_main_queue(), ^{
        
         [self.shopTableView reloadData];
    });
   
    
    if (_shopingArr.count == 0)
    {
        [self.shopTableView removeFromSuperview];
        [self noThingView];
        [self initMyNavigationBar];
    }
   
}
#pragma mark -- 懒加载
- (NSMutableArray *)shopingArr
{
    if (!_shopingArr)
    {
        _shopingArr = [[NSMutableArray alloc]init];
    }
    return _shopingArr;
}
- (UITableView *)shopTableView
{
    if (!_shopTableView)
    {
        _shopTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64)];
        _shopTableView.delegate = self;
        _shopTableView.dataSource = self;
        [_shopTableView registerNib:[UINib nibWithNibName:@"ShotTableViewCell" bundle:nil] forCellReuseIdentifier:@"SHOTCELL"];
        ShopHeaderView *header = [ShopHeaderView sharedShopHeaderView];
        header.frame = CGRectMake(0, 0, self.view.frame.size.width, 40);
        [header createHeaderView];
        _shopTableView.tableHeaderView = header;
        
        UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 70)];
        UIButton *jiesuanBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 20, 50)];
        jiesuanBtn.center = footView.center;
        [jiesuanBtn setTitle:@"立即结算" forState:UIControlStateNormal];
        [jiesuanBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        jiesuanBtn.backgroundColor = [UIColor blueColor];
        [jiesuanBtn addTarget:self action:@selector(jiesuanBtnC) forControlEvents:UIControlEventTouchUpInside];
        [footView addSubview:jiesuanBtn];
        _shopTableView.tableFooterView = footView;
        
    }
    return _shopTableView;
}

- (void)jiesuanBtnC
{
    AddressViewController *add = [[AddressViewController alloc]init];

    [self.navigationController pushViewController:add animated:YES];
   


}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    self.tabBarController.tabBar.hidden=NO;
    self.navigationController.navigationBar.hidden = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
